package com.youtills.view;

import com.youtills.resource.HttpResourceController;
import com.youtills.resource.HttpResponse;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;

/**
 * TODO - !!!doc me!!!
 *
 * @author ravi.chiluveru
 * @since 23.0 (7/28/14)
 */
public class HttpJsonResponseWriter extends JsonResponseWriter implements HttpResponseWriter {

    private final HttpResourceController controller;

    public HttpJsonResponseWriter(HttpResourceController controller) {
        this.controller = controller;
    }

    public void writeResponse(){
        final HttpResponse response = this.controller.execute();
        final HttpServletResponse httpServletResponse = controller.getResponse();

        if(response.getCookies() != null){
            for(Cookie cookie: response.getCookies()){
                httpServletResponse.addCookie(cookie);
            }
        }

        httpServletResponse.setStatus(response.getStatus());
    }
}
